﻿using CL.Business.BLL;
using CL.Business.Entity;
using CL.Enum.Common;
using CL.Enum.Common.Union;
using CL.Tools.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CL.Admin.admin.promotion
{
    public partial class User_review_Aduit : UI.AdminPage
    {
        Log log = new Log("User_review_Aduit");
        protected static int AccountID = 0;
        protected AccountEntity accountEntity = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int.TryParse(Request.QueryString["AccountID"], out AccountID);
                if (AccountID > 0)
                    BindData();
                else
                {
                    btnNoPass.BackColor = Color.Gray;
                    btnAuditing.BackColor = Color.Gray;
                    btnNoPass.Enabled = false;
                    btnAuditing.Enabled = false;
                }
            }
        }

        protected void BindData()
        {
            try
            {
                #region 用户信息数据
                accountEntity = new AccountBLL().QueryEntity(AccountID);
                if (accountEntity == null)
                {
                    btnNoPass.BackColor = Color.Gray;
                    btnAuditing.BackColor = Color.Gray;
                    btnNoPass.Enabled = false;
                    btnAuditing.Enabled = false;
                    ClientScript.RegisterStartupScript(this.GetType(), "script", "alert('未查询到用户信息，请检查');", true);
                    return;
                }

                if ( (int)AccountStatusEnum.normal == accountEntity.AccountStatus ||
                    (int)AccountStatusEnum.noPass == accountEntity.AccountStatus )
                {
                    btnNoPass.BackColor = Color.Gray;
                    btnNoPass.Enabled = false;
                    btnAuditing.BackColor = Color.Gray;
                    btnAuditing.Enabled = false;
                }

                lbAgencyAcccount.InnerText = accountEntity.AccountCode;
                lbBankCard.InnerText = accountEntity.BankCard;
                lbCreateTime.InnerText = accountEntity.CreateTime.ToString("yyyy-MM-dd HH:mm:ss");
                lbFullName.InnerText = accountEntity.FullName;
                lbIdentity.InnerText = accountEntity.IdentityID;

                string imgHost = ConfigHelper.GetConfigString("ICONHOSTURL");
                accountEntity.FirstIdentityImage = imgHost + accountEntity.FirstIdentityImage;
                accountEntity.SecondIdentityImage = imgHost + accountEntity.SecondIdentityImage;
                accountEntity.BankCardImage = imgHost + accountEntity.BankCardImage;
                #endregion
            }
            catch (Exception ex)
            {
                log.Write(string.Format("加载数据发生错误：{0}", ex.StackTrace), true);
                ClientScript.RegisterStartupScript(this.GetType(), "script", "alert('用户信息资料错误，请重修改');", true);
            }
        }

        
        /// <summary>
        /// 审核不通过
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnNoPassing_Click(object sender, EventArgs e)
        {
            bool result = new AccountBLL().AccountInfoAuditing(AccountID, (int)AccountStatusEnum.noPass);
            if (result)
            {
                //发送审核消息
                accountEntity = new AccountBLL().QueryEntity(AccountID);
                SendMsgToAgent(AccountID, "申请审核未通过", string.Format("推广员 {0} 你好，你提交的身份证照片或者银行卡照片不清晰，未能够通过审核。请重新上传后提交审核", accountEntity.AccountCode));
                JscriptMsg("设置'不通过'成功!", "User_review.aspx");
            }
            else
            {
                JscriptMsg("审核失败，请联系管理员", "User_review.aspx");
            }
        }

        protected void SendMsgToAgent(int accountID, string msTitle, string msBody)
        {
            NoticeBLL notice = new NoticeBLL();
            NoticeEntity entity = new NoticeEntity();
            entity.AccountID = accountID;
            entity.IsRead = false;
            entity.NoticeType = 1;
            entity.ReadTime = DateTime.Now.AddMinutes(30);
            entity.SendTime = DateTime.Now;
            entity.NoticeTitle = msTitle;
            entity.NoticeContent = msBody;
            notice.InsertNotice(entity);
        }

        /// <summary>
        /// 通过审核
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAuditing_Click(object sender, EventArgs e)
        {
            bool result = new AccountBLL().AccountInfoAuditing(AccountID, (int)AccountStatusEnum.normal);
            if (result)
            {
                accountEntity = new AccountBLL().QueryEntity(AccountID);

                //添加返点率
                RebateEntity entity = new RebateEntity();
                entity.Amount = 99;
                entity.CreateTime = DateTime.Now;
                entity.EfficaciousTime = DateTime.Now;
                entity.RebateNum = 8;
                entity.SettlementNum = 200;
                entity.TaxPoint = 0;
                entity.AccountID = AccountID;
                entity.ModifyTime = entity.ModifyTime = DateTime.Now;
                entity.EfficaciousTime = DateTime.Now.AddYears(1);
                if (new RebateBLL().InsertEntity(entity) > 0)
                {
                    new RecordBLL().InsertEntity(new Business.Entity.RecordEntity()
                    {
                        AccountID = entity.AccountID,
                        RecordTime = DateTime.Now,
                        RecordRemark = string.Format("修改返点"),
                        RecordType = 4
                    });
                    AddAdminLog(CaileEnums.ActionEnum.Edit.ToString(), "增加代理人返点设置:" + accountEntity.AccountCode); //记录日志

                    SendMsgToAgent(AccountID, "申请审核已通过", string.Format("推广员 {0} 你好，你的推广信息资料已通过审核，可以开始推广赚取佣金了!", accountEntity.AccountCode));
                    JscriptMsg("审核完成", "User_review.aspx");
                }
                else
                {
                    JscriptMsg("添加代理会员返点失败, ", "User_review.aspx");
                }
            }
            else
            {
                JscriptMsg("审核失败，请联系管理员", "User_review.aspx");
            }
        }

        protected string ShowAgentAccountType(int accountStatus)
        {
            if ((int)AccountStatusEnum.disable == accountStatus)
                return "禁用";
            else if ((int)AccountStatusEnum.certification == accountStatus)
                return "待验证";
            else if ((int)AccountStatusEnum.noPass == accountStatus)
                return "未通过";
            else if ((int)AccountStatusEnum.normal == accountStatus)
                return "通过";
            else if ((int)AccountStatusEnum.toBeAudited == accountStatus)
                return "待上传";
            else
                return "未知状态";
        }
    }
}